package com.ljtxxhwj.myblob.service.impl;

import com.ljtxxhwj.myblob.entity.Article;
import com.ljtxxhwj.myblob.entity.Like;
import com.ljtxxhwj.myblob.mapper.LikeMapper;
import com.ljtxxhwj.myblob.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {

    private final LikeMapper likeMapper;

    @Autowired
    public LikeServiceImpl(LikeMapper likeMapper) {
        this.likeMapper = likeMapper;
    }
@Override
public List<Article> selectLikedArticlesByUserId(int userId)
{return likeMapper.selectLikedArticlesByUserId(userId);}
    @Override
    public List<Like> selectLikesByUserId(Integer userId) {
        return likeMapper.selectLikesByUserId(userId);
    }
    @Override
    public int getTotalLikesByUserId(int userId) {
        // 调用Mapper层的方法来获取点赞总数
        return likeMapper.countLikesByUserId(userId);
    }
    @Override
    public List<Like> selectLikesByArticleId(Integer articleId) {
        return likeMapper.selectLikesByArticleId(articleId);
    }

    @Override
    public void insertLike(Like like) {
        if (like != null) {
//            like.setLikeDate(LocalDateTime.now());
            like.setLikeDate(new Timestamp(new Date().getTime()));
            likeMapper.insertLike(like);
        }
    }

    @Override
    public void deleteLikeByArticleAndUser(Integer articleId, Integer userId) {
        likeMapper.deleteLikeByArticleAndUser(articleId, userId);
    }

    @Override
    public List<Like> selectLiked(Integer userId) {
        return likeMapper.selectLiked(userId);
    }
}